package com.eastfair.auth.controller;

import com.eastfair.auth.dto.UserOrganizationDTO;
import com.eastfair.auth.dto.UserOrganizationPageQuery;
import com.eastfair.auth.dto.UserOrganizationSaveDTO;
import com.eastfair.auth.dto.UserOrganizationUpdateDTO;
import com.eastfair.auth.service.UserOrganizationService;
import com.eastfair.auth.dto.*;
import com.eastfair.auth.entity.UserOrganization;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author ligang
 * @date 2021-07-16
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/userOrganization")
@Api(value = "UserOrganization", tags = "")
public class UserOrganizationController extends SuperController<UserOrganizationService, Long, UserOrganization, UserOrganizationPageQuery, UserOrganizationSaveDTO, UserOrganizationUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<UserOrganization> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<UserOrganization> userOrganizationList = list.stream().map((map) -> {
            UserOrganization userOrganization = UserOrganization.builder().build();
            //TODO 请在这里完成转换
            return userOrganization;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(userOrganizationList));
    }


    /**
     * 添加用户到组织
     * @param userOrganizationDTO 用户组织实体
     * @return
     */
    @ApiOperation(value = "添加用户到组织", notes = "添加用户到组织")
    @PostMapping("/addUserOfOrg")
    public R<UserOrganization> addUserOfOrg(@RequestBody @Validated({UserOrganizationDTO.VerityUserOfOrg.class }) UserOrganizationDTO userOrganizationDTO) {
        if(userOrganizationDTO.getProjectId()==null){
            userOrganizationDTO.setProjectId(ContextUtil.getProjectId());
        }
        return baseService.addUserOfOrg(userOrganizationDTO.getOrganizationId(),userOrganizationDTO.getUserIds(),userOrganizationDTO.getProjectId());
    }
    /**
     * 剔除组织的主办管理员
     * @param userOrganizationDTO 用户组织实体
     * @return
     */
    @ApiOperation(value = "剔除组织的主办管理员", notes = "剔除组织的主办管理员")
    @PostMapping("/removeUserOfOrg")
    public R removeUserOfOrg(@RequestBody UserOrganizationDTO userOrganizationDTO) {
        return baseService.removeUserOfOrg(userOrganizationDTO.getOrganizationId(),userOrganizationDTO.getUserIds());
    }


}
